package org.springframework.license.exception;

import com.alibaba.fastjson.JSONObject;
import lombok.Getter;

/**
 * @Title LicenseException
 * @Author zhijun.Guo
 * @Email tiankong061015@163.com
 * @Time 2024/9/3 12:55
 * @Description
 */
@Getter
public class LicenseException extends RuntimeException{

    private Integer code;

    private String message;

    @Getter
    public enum Error{

        CERTIFICATE_INSTALL_FAILED(99001, "授权证书安装失败!"),
        CERTIFICATE_AUTHORIZATION_EXPIRED(99002, "授权过期!"),
        CERTIFICATE_AUTHORIZATION_FAILED(99003, "证书验证失败!")
        ;

        Error(Integer code, String message) {
            this.code = code;
            this.message = message;
        }

        private final Integer code;

        private final String message;

    }

    /**
     * Constructs a new runtime exception with {@code null} as its
     * detail message.  The cause is not initialized, and may subsequently be
     * initialized by a call to {@link #initCause}.
     */
    public LicenseException() {
        super();
    }

    /**
     * Constructs a new runtime exception with the specified detail message.
     * The cause is not initialized, and may subsequently be initialized by a
     * call to {@link #initCause}.
     *
     * @param message the detail message. The detail message is saved for
     *                later retrieval by the {@link #getMessage()} method.
     */
    public LicenseException(String message) {
        super(message);
    }

    public LicenseException(Error error) {
        super(error.getMessage());
        this.code = error.getCode();
        this.message = error.getMessage();
    }

    public LicenseException(Integer code, String message) {
        super(message);
        this.code = code;
        this.message = message;
    }

    @Override
    public String toString(){
        return JSONObject.toJSONString(this);
    }
}
